package com.liu.controller;

import com.liu.domain.VoteTopic;
import com.liu.domain.text;
import com.liu.repository.VotetopicRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by liu on 17-10-27.
 */
@RestController
public class VotetopicController {

    private final static Logger logger = LoggerFactory.getLogger(VotetopicController.class);

    @Autowired
    private VotetopicRepository VoteTopicRepository;

    /*
    查询所有
     */
    @GetMapping(value = "/VT")
    public List<VoteTopic> textList() {
//        System.out.println("textList");
//
//        logger.info("textList");

        return VoteTopicRepository.findAll();
        // return "{\"code\":0,\"msg\":\"\",\"count\":1000,\"data\":" + textRepository.findAll() + "}";

    }

    /*
   查询日期最大的
    */
    @GetMapping(value = "/VTZ")
    public VoteTopic textList1() {

        List<VoteTopic> topics = VoteTopicRepository.findBytime();
        return topics.get(0);
    }

    /*
    添加一个
     */
    @PostMapping(value = "/VT")
    public ModelAndView VTAdd(VoteTopic vt) {
        VoteTopicRepository.save(vt);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("addVote");
        mav.addObject("flag", 1);
        System.out.println("flag");
        return mav;
    }

    /*
    更新一个的信息
     */
    @PutMapping(value = "/VT/{id}")
    public VoteTopic studentUpdate(@PathVariable("id") int id, VoteTopic VT) {

        return VoteTopicRepository.save(VT);
    }
    /*
    删除一个
     */

    @DeleteMapping(value = "/VT/{id}")
    public void studentDelete(@PathVariable("id") int id) {
        VoteTopicRepository.delete(id);
    }
}
